home *** CD-ROM | disk | FTP | other *** search
- Path: news.nask.org.pl!usenet
- From: piotrpar@blue.maloka.waw.pl (Piotr Parlewicz)
- Newsgroups: comp.lang.c++
- Subject: Re: Can somebody explain me how to do this in C++
- Date: Wed, 06 Mar 1996 23:39:02 GMT
- Organization: Research and Academic Computer Network
- Message-ID: <4hl7ib$cpi@bilbo.nask.org.pl>
- References: <4hkb2o$ko3@mo6.rc.tudelft.nl>
- NNTP-Posting-Host: s110.maloka.waw.pl
- X-Newsreader: Forte Free Agent 1.0.82
-
- Ejo Schrama <schrama@geo.tudelft.nl> wrote:
-
- >Suppose you've got a base class containing protected data and several
- >public methods among which there are some virtual ones. This base class
- >is inhereted to "lets call them" subclasses in which the virtual methods
- >are implemented. All subclasses are defined during the initialization of a
- >problem, however during the execution of a program "where all work is
- >executed" you only want to deal with a linked list of base class objects
- >which are accessed via a while loop. The code looks as follows:
-
- > Cparam *localpointer = root_of_linked_list;
- > while (localpointer != NULL) {
- > localpointer->execute_some_virtual_method();
- > localpointer = localpointer->getlinktonext();
- > }
-
- >So far there is no problem until the following situation happens. There are
- >local variables declared as protected data inside the inhereted subclasses
- >(which don't exist in the baseclass) and I want to access those variables
- >in the while construction described above.
-
- >Any attempt I do to declare
-
- > Cparam_inhereted_method *localpointer2 = localpointer;
-
- >will always fail since:
-
- > class Cparam {
- > protected:
- > <various data>
- > public:
- > <various constructors/destructors and methods except execute_routine>
- > virtual void execute_some_virtual_method();
- > };
-
- > class Cparam_inhereted_method : public Cparam {
- > protected:
- > <various data>
- > public:
- > <constructors/destructors>
- > void execute_some_virtual_method();
- > void execute_routine();
- > };
-
- >so that I can never execute
-
- > localpointer2->execute_routine();
-
- >The only alternative seems to introduce execute_routine to the Cparam
- >base class (which I would like to avoid). How can I avoid this?
-
- If I understand your example correctly it seems that all you were
- missing was an explicit type cast to the derived type.
- > Cparam_inhereted_method *localpointer2 = localpointer;
- would work as :
- > Cparam_inhereted_method *localpointer2 = (Cparam_inhereted_method *)localpointer;
-
- However this can be risky, if you have no way of selecting the correct
- derived type.
- Here is a working example (BC4.51)
-
-
- #include <stdio.h>
-
- class CBase {
- protected:
- char d ;
- public:
- void m1( ) {
- printf("m1 in base\n") ;
- }
-
- virtual void m2( ) {
- printf("m2 in base\n");
- }
- } ;
-
- class CDer : CBase {
- protected:
- char e;
- public:
- void m3( ) {
- printf("M3 in Derived\n");
- }
-
- void m2() {
- printf("M2 in Derived...\n");
- }
-
- };
-
-
- class CDer2 : CBase {
- protected:
- char f;
- int g ;
-
- public:
- void m3( ) {
- printf("M3 in Derived2\n");
- }
-
- void m2() {
- printf("M2 in Derived2...\n");
- }
-
- };
-
-
-
- int main( int argc, char **argv ) {
-
- CDer cd ; // cd will be object of derived type
- CDer2 cd2 ;
-
- CBase *cb = (CBase*)&cd ; // cb will be generic base class pointer
- CDer *cdp = (CDer *)cb ; // now do your cast to more complex (derived)
- type
- cdp->m2(); // check if virtual function knows we are really of derived
- type
- cdp->m3(); // check if function existing only in derived works
-
- cb = (CBase*)&cd2 ; // cb will be generic base class pointer
- CDer2 *cd2p = (CDer2 *)cb ; // now do your cast to more complex
- (derived) type
- cd2p->m2(); // check if virtual function knows we are really of
- derived type
- cd2p->m3(); // check if function existing only in derived works
- }
-
- Piotr Parlewicz
- (piotrpar@blue.maloka.waw.pl)
-
-
-
-